package com.cakebackoffice;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.cakebackoffice.Model.Address;
import com.cakebackoffice.Model.Client;
import com.cakebackoffice.Model.Order;
import com.cakebackoffice.Model.UnitarySale;

import java.util.Calendar;
import java.util.LinkedList;

public class FormActivity extends Activity {


    //public static List<Product> TemporaryOrderList = new ArrayList<Product>();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.createform);

    }


    //Récupère la commande
    private Order getOrder() {
        Client c = new Client(
                getClientName(),
                getClientFirstName(),
                getClientNumber()
        );
        Address a = new Address(
                getLine1(),
                getLine2(),
                getLine3(),
                getLine4(),
                getCity(),
                getPostalCode(),
                getCountry()
        );
        Order order = new Order(c, a, new LinkedList<UnitarySale>(), Calendar.getInstance().getTime());
        return order;
    }


    //Récupère le nom du ClientTable
    private String getClientName() {
        EditText editText = (EditText) findViewById(R.id.Name);
        return editText.getText().toString();
    }

    //Récupère le prénom du ClientTable
    private String getClientFirstName() {
        EditText editText = (EditText) findViewById(R.id.FirstName);
        return editText.getText().toString();
    }

    //Récupère le numéro du ClientTable
    private String getClientNumber() {
        EditText editText = (EditText) findViewById(R.id.Number);
        return editText.getText().toString();
    }

    //Récupère les 4 lignes de l'adresse du ClientTable
    private String getLine1() {
        EditText editText = (EditText) findViewById(R.id.line1E);
        return editText.getText().toString();
    }

    private String getLine2() {
        EditText editText = (EditText) findViewById(R.id.line2E);
        return editText.getText().toString();
    }

    private String getLine3() {
        EditText editText = (EditText) findViewById(R.id.line3E);
        return editText.getText().toString();
    }

    private String getLine4() {
        EditText editText = (EditText) findViewById(R.id.line4E);
        return editText.getText().toString();
    }

    //Récupère la rue du ClientTable
    private String getCity() {
        EditText editText = (EditText) findViewById(R.id.City);
        return editText.getText().toString();
    }

    //Récupère le code postal du ClientTable
    private String getPostalCode() {
        EditText editText = (EditText) findViewById(R.id.PostalCode);
        return editText.getText().toString();
    }

    //récupère le pays du ClientTable
    private String getCountry() {
        EditText editText = (EditText) findViewById(R.id.Country);
        return editText.getText().toString();
    }


    //Enregistre les details adresse et client
    public void save(View v) {
        Toast.makeText(this, "Client enregistré. Sélectionner les produits !", Toast.LENGTH_SHORT).show();

        Order order = this.getOrder();
        order.saveToDatabase(this);

        Intent intent = new Intent(this, FormActivity2.class);
        intent.putExtra("orderid", order.getIdDB());
        startActivity(intent);

    }


    //Annule la commande
    public void cancel(View view) {
        finish();
    }

}


